const express = require('express')
const ejs = require('ejs')
const app = express()
const config = require('./config/default.json')
const bodyParser = require('body-parser')

// 链接数据库 MongoDB
require('./config/db.js')()
// 配置跨域
app.all('*', (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Headers', 'Content-Type')
    res.header('Content-Type', 'application/json;charset=utf-8')
    next()
})
// 中间件解析json express.json || bodyParser.json
app.use(bodyParser.json())

// 路由
app.use('/', require('./routes/index.js'))
app.use('/api/url', require('./routes/url.js'))

// ejs
// 配置html引擎
app.engine('html', ejs.__express)
// 设置视图引擎
app.set('views', './views') // 设置目录
app.set('view engine', 'html')
// 配置静态资源路径
app.use(express.static('./public'))

// 监听端口
// const processArgv = [...process.argv]
// const port = processArgv.indexOf("--port")
// const __PORT__ = port > 0 ? processArgv[port + 1] : 80
const __PORT__ = config.port
app.listen(__PORT__, () => {
    console.log('server is running at ' + __PORT__)
})